/**
 *  GTUG Zaragoza
 *  http://zaragoza.gtugs.org/
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.gtugs.zaragoza.feeds;

import org.gtugs.zaragoza.R;
import org.gtugs.zaragoza.feeds.provider.FeedEntryContentProvider;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.widget.TextView;

public class FeedActivity extends Activity {

	public static final String FEED_ID = "FEED_ID";
	private Cursor cursor;
	private TextView mAuthor;
	private TextView mTitle;
	private TextView mBodyText;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_PROGRESS);
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		setContentView(R.layout.feed_activity);

		// 
		mAuthor = (TextView) findViewById(R.id.itemAuthor);
		mTitle = (TextView) findViewById(R.id.itemTitle);
		mBodyText = (TextView) findViewById(R.id.bodyText);
		
		// Para que gestione los posibles links html del texto
		mBodyText.setMovementMethod(LinkMovementMethod.getInstance());

		super.onCreate(savedInstanceState);
	}

	@Override
	protected void onResume() {
		long feedId = 0;
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			feedId = extras.getLong(FEED_ID);
			Log.i("RECEIVED", String.valueOf(feedId));
		}

		cursor = managedQuery(Uri
				.parse(FeedEntryContentProvider.CONTENT_ID_URI_BASE
						+ String.valueOf(feedId)), null, null, null, null);
		if (cursor.moveToFirst()) {
			int columnIndex = cursor
					.getColumnIndexOrThrow(FeedEntry.AUTHOR_NAME);
			mAuthor.setText(cursor.getString(columnIndex));

			columnIndex = cursor.getColumnIndexOrThrow(FeedEntry.TITLE);
			mTitle.setText(cursor.getString(columnIndex));

			columnIndex = cursor.getColumnIndexOrThrow(FeedEntry.SUMMARY);
			
			mBodyText.setText(Html.fromHtml(cursor.getString(columnIndex)));
		}
		super.onResume();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		 MenuInflater inflater = getMenuInflater();
		 inflater.inflate(R.menu.feed_menu, menu);
		 return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()) {
		case R.id.option_share:
			
			String subject = cursor.getString(cursor.getColumnIndexOrThrow(FeedEntry.TITLE));
			String text = cursor.getString(cursor.getColumnIndexOrThrow(FeedEntry.LINK));
			
			Intent intent = new Intent();
			intent.setType("text/plain");
			intent.putExtra(Intent.EXTRA_SUBJECT, subject);
			intent.putExtra(Intent.EXTRA_TEXT, text);

			startActivity(Intent.createChooser(intent, getString(R.string.tit_share)));

		}
		return super.onOptionsItemSelected(item);
	}

}
